package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.EmployeeLoginService;
import com.clqteam.service.InsertEmployeeService;
import com.clqteam.service.ShowEmployeeService;
import com.clqteam.service.ShowrNameService;
import com.clqteam.serviceimpl.EmployeeLoginServiceImpl;
import com.clqteam.serviceimpl.InsertEmployeeServiceImpl;
import com.clqteam.serviceimpl.ShowEmployeeServiceImpl;
import com.clqteam.serviceimpl.ShowrNameServiceImpl;

/**
 * 
 * @author CLQ
 *
 */
public class InsertEmployeeController extends HttpServlet{
	/**
	 * 添加员工信息
	 */
     InsertEmployeeService inService=new InsertEmployeeServiceImpl();
     EmployeeLoginService emService =new EmployeeLoginServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //设置字符集
	   req.setCharacterEncoding("utf8");
       resp.setCharacterEncoding("utf8");
       //获取页面的ajax传的值
	   String account=req.getParameter("eAccount");
	   String psw=req.getParameter("ePsw");
	   String name=req.getParameter("eName");
	   int rname1=Integer.parseInt(req.getParameter("rname"));
	   //调用service层的方法
	   boolean b=inService.insertEmployeeInfo(account, psw, name, rname1);
	   //回写
	    PrintWriter pw=resp.getWriter();
	     if(b){
	    	 HttpSession session=req.getSession();
			  Employee emp=(Employee)session.getAttribute("employee");
			  SimpleDateFormat sd=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
			  Date date=new Date();
			  String time=sd.format(date);
			  String info="添加新员工："+name;
			  emService.insertLoginTime(time, info, emp.geteId(),emp.geteName());
	    	 pw.write("1");
	     }else{
	    	 pw.write("0");
	     }
	     //清理和关闭
	     pw.flush();
	     pw.close();
	}
}
